package com.example.Hongbao;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.util.Log;
import android.view.accessibility.AccessibilityManager;

import java.util.Iterator;
import java.util.List;

/**
 * Created by Fcy on 2015/12/24.
 */
public class AccessServiceManager {
    private static boolean DEBUG = false;

    private static String TAG = "AccessibilityManager";

    /*
     * Check Accessibility Setting, IF NOT enabled, Notice User To Enable
	 *
	 */
    public static boolean isAccessibilityEnabled(Context paraContext) {
        //
        Log.i(TAG, "Enter isAccessibilityEnabled");
        if (paraContext == null) {
            Log.e(TAG, "isAccessibilityEnabled paraContext Null");
            return false;
        }
        AccessibilityManager accessibilityManager = (AccessibilityManager) paraContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (accessibilityManager == null) {
            Log.e(TAG, "Accessibility service Null");
            return false;
        }
        String selfPackageName = paraContext.getPackageName();

        // TODO: 可以monitor服务状态变化，以及时启动服务
        // API Level 18, Do not use
        // accessibilityManager.addTouchExplorationStateChangeListener(listener);

        // Get Enabled services
        // Sometimes, even settings display checked, But in fact the service is not enabled(maybe APP changed)
        List<AccessibilityServiceInfo> enableList =
            accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC);

        if (enableList == null) {
            if (DEBUG) {
                Log.w(TAG, "Accessibility Service Not enabled");
            }
             /*
             * 2015-4-8：调用者发送变化，会循环进行判断，因此不再上报信息
			Analytics.analyticsOnEvent(paraContext, Analytics.FIAL_AS_NOT_ENABLE);
			*/
            return false;
        }
        Iterator<AccessibilityServiceInfo> iterator = enableList.iterator();
        while (iterator.hasNext()) {
            AccessibilityServiceInfo asInfo = (AccessibilityServiceInfo) iterator.next();
            if (asInfo.getResolveInfo() == null) {
                if (DEBUG) {
                    Log.w(TAG, "getResolveInfo NULL");
                }
                continue;
            }
            if (selfPackageName.equalsIgnoreCase(asInfo.getResolveInfo().serviceInfo.packageName)) {
                return true;
            } else {
                 /*
                 * 2015-4-8：调用者发送变化，会循环进行判断，因此不再上报信息
				Map<String, String> enableAs = new HashMap<String, String>();
				enableAs.put("enabled", asInfo.getResolveInfo().serviceInfo.packageName);
				Analytics.analyticsOnEvent(paraContext, Analytics.ENABLED_ACCESSIBILITY_SERVICE, enableAs);
				*/
            }
        }
        if (DEBUG) {
            Log.w(TAG, "Accessibility Service Not enabled");
        }
        /*
         * 2015-4-8：调用者发送变化，会循环进行判断，因此不再上报信息
		Analytics.analyticsOnEvent(paraContext, Analytics.FIAL_AS_NOT_ENABLE);
		*/
        return false;
    }
}
